﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Threading;

namespace SanctusSoftware.CcliSongSelectLib.Wpf
{
    /// <summary>
    /// Interaction logic for SongSelectSearchWindow.xaml
    /// </summary>
    public partial class SongSelectSearchWindow : Window, INotifyPropertyChanged
    {

        #region Events

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region Properties

        private SongSelectDocument[] _Documents = null;
        public SongSelectDocument[] Documents
        {
            get { return _Documents; }
            set
            {
                _Documents = value;
                OnPropertyChanged("Documents");
            }
        }

        private SongSelectDocument[] _FoundDocuments = null;
        public SongSelectDocument[] FoundDocuments
        {
            get { return _FoundDocuments; }
            private set
            {
                _FoundDocuments = value;
                OnPropertyChanged("FoundDocuments");
            }
        }

        private SongSelectDocument[] _SelectedDocuments = null;

        public SongSelectDocument[] SelectedDocuments
        {
            get { return _SelectedDocuments; }
            private set
            {
                _SelectedDocuments = value;
                OnPropertyChanged("SelectedDocuments");
            }
        }

        private string _SearchAuthor = string.Empty;
        public string SearchAuthor
        {
            get { return _SearchAuthor; }
            set
            {
                _SearchAuthor = value;
                OnPropertyChanged("SearchAuthor");
            }
        }

        private string _SearchTheme = string.Empty;
        public string SearchTheme
        {
            get { return _SearchTheme; }
            set
            {
                _SearchTheme = value;
                OnPropertyChanged("SearchTheme");
            }
        }

        private string _SearchTitle = string.Empty;
        public string SearchTitle
        {
            get { return _SearchTitle; }
            set
            {
                _SearchTitle = value;
                OnPropertyChanged("SearchTitle");
            }
        }

        private string _SearchWords = string.Empty;
        public string SearchWords
        {
            get { return _SearchWords; }
            set
            {
                _SearchWords = value;
                OnPropertyChanged("SearchWords");
            }
        }

        #endregion

        #region Constructor

        public SongSelectSearchWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        #endregion

        #region Methods (Event Handlers)

        private delegate void NoArgsDelegate();

        private void SearchCriterion_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (_SearchAsITypeCheckBox.IsChecked == true)
            {
                NoArgsDelegate del = delegate()
                {
                    try
                    {
                        FoundDocuments = SongSelectDocumentSearcher.Search(Documents, SearchTitle, SearchAuthor, SearchTheme, SearchWords);
                    }
                    catch (Exception ex)
                    { 
                    }
                };
                Thread t = new Thread(new ThreadStart(del));
                t.Start();
            }
        }


        private void _SearchButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                FoundDocuments = SongSelectDocumentSearcher.Search(Documents, SearchTitle, SearchAuthor, SearchTheme, SearchWords);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }

        private void _CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }

        private void _OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (dataGrid1.SelectedItems.Count == 0)
            {
                MessageBox.Show("Please select one or more songs.");
            }
            else
            {
                List<SongSelectDocument> list = new List<SongSelectDocument>();
                foreach (SongSelectDocument doc in dataGrid1.SelectedItems)
                {
                    list.Add(doc);
                }
                SelectedDocuments = list.ToArray();

                this.DialogResult = true;
                this.Close();
            }
            
        }

        #endregion

        #region Helper

        private static bool In(object value, params object[] args)
        {
            foreach (object item in args)
            {
                if (item == value)
                {
                    return true;
                }
            }
            return false;
        }

        #endregion



    }
}
